home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / CAD_CAM / A7221V1B / INOUT.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  12KB  |  385 lines

  1. /*
  2.    Module:  inout.c
  3.    Date:    3/9/92
  4.    Version: 1.0b
  5.    Author:  Dave Lutz
  6.    Email:   lutz@psych.rochester.edu
  7.    Copyright: 1992 University of Rochester, Psychology Dept.
  8.  
  9.    Disclaimer:  This software is distributed free of charge.  As such, it
  10.                 comes with ABSOLUTELY NO WARRANTY.  The user of the software
  11.                 assumes ALL RISKS associated with its use.
  12.  
  13.                 Your rights to modify and/or distribute this software are
  14.                 outlined in the file ADI7221.DOC.
  15.  
  16.    Purpose: This module provides the input/output routines for the ADI
  17.             converter.  It provides the routines needed by the driver
  18.             to open input and output files, and read data.  It also
  19.             provides the output funtion that can be used by the device
  20.             dependant routines to send their output to the file opened
  21.             by the driver.
  22.  
  23.    Functions Provided:
  24.  
  25.             openin
  26.             openout
  27.             closein
  28.             closeout
  29.             getstr
  30.             putarr
  31.  
  32.    Functions Required:
  33.  
  34.             none
  35.  
  36. */
  37.  
  38.  
  39.  
  40.  
  41. #include <fcntl.h>
  42. #include <sys\types.h>
  43. #include <sys\stat.h>
  44. #include <stdlib.h>
  45. #include <malloc.h>
  46. #include <io.h>
  47. #include "retcodes.h"
  48. #include "inout.h"
  49.  
  50.  
  51. /*
  52.    Function: openin
  53.    Purpose:  Allocate the input buffer and open the input file for reading.
  54.  
  55.    Pre: name is a pointer to an ascii string containing the name of the 
  56.         input file.
  57.         pltfpp is a pointer to storage for a pointer to a PLTFILE.
  58.  
  59.    Post:  The input buffer is allocated.
  60.           The input file is opened for reading.
  61.           If the buffer can't be allocated, *pltfpp is set to NULL and
  62.           NOBUFF is returned.
  63.           If the input file can't be opened, *pltfpp is set to NULL and
  64.           BADOPEN is returned.
  65.           Otherwise, TRUE is returned.
  66. */
  67.  
  68. int openin (name,pltfpp)
  69.    char *name;
  70.    PLTFILE **pltfpp;
  71. {
  72.    /* first try to allocate a PLTFILE */
  73.    *pltfpp = (PLTFILE *) malloc (sizeof (PLTFILE));
  74.    if (*pltfpp == (PLTFILE *)NULL)
  75.       return (NOBUFF);
  76.  
  77.    /* now try to allocate the buffer */
  78.    (*pltfpp)->buf = (char *) malloc ((size_t)(BUFFSIZE*sizeof(char)));  
  79.    if ((*pltfpp)->buf == (char *)NULL) {
  80.       free ((void *)*pltfpp);
  81.       *pltfpp = (PLTFILE *)NULL;
  82.       return (NOBUFF);
  83.    }
  84.  
  85.    /* now attempt to open the input file */
  86.    (*pltfpp)->fd = open (name,O_RDONLY|O_TEXT);
  87.    if ((*pltfpp)->fd < 0) {
  88.       free ((void *)(*pltfpp)->buf);
  89.       free ((void *)*pltfpp);
  90.       *pltfpp = (PLTFILE *)NULL;
  91.       return (BADOPEN);
  92.    }
  93.  
  94.    /* now set buf_ctr to zero so the first read will cause the buffer to be 
  95.       filled from the input file
  96.    */
  97.    (*pltfpp)->buf_ctr = 0;
  98.  
  99.    (*pltfpp)->buf_ptr = (*pltfpp)->buf;
  100.    (*pltfpp)->eof=FALSE;
  101.    return (TRUE);
  102. }
  103.  
  104.  
  105.  
  106. /*
  107.    Function: openout
  108.    Purpose:  Allocate the output buffer, and open the output file for
  109.              writing.
  110.  
  111.    Pre: name is a pointer to an ascii string containing the name of the
  112.         output file.
  113.         pltfpp is a pointer to storage for a pointer to a PLTFILE.
  114.  
  115.    Post:  The output buffer is allocated.
  116.           The output file is opened for writing.
  117.           If the output buffer can't be allocated, *pltfpp is set to NULL and
  118.           NOBUFF is returned.
  119.           If the output file can't be opened, *pltfpp is set to NULL and
  120.           BADOPEN is returned.
  121.           Otherwise, TRUE is returned.
  122. */
  123.  
  124. int openout (name,pltfpp)
  125.    char *name;
  126.    PLTFILE **pltfpp;
  127. {
  128.    /* first try to allocate a PLTFILE */
  129.    *pltfpp = (PLTFILE *) malloc (sizeof (PLTFILE));
  130.    if (*pltfpp == (PLTFILE *)NULL)
  131.       return (NOBUFF);
  132.  
  133.    /* now try to allocate the buffer */
  134.    (*pltfpp)->buf = (char *) malloc ((size_t) (BUFFSIZE*sizeof(char)));  
  135.    if ((*pltfpp)->buf == (char *)NULL) {
  136.       free ((void *)*pltfpp);
  137.       *pltfpp = (PLTFILE *)NULL;
  138.       return (NOBUFF);
  139.    }
  140.  
  141.    /* now attempt to open the output file */
  142.    (*pltfpp)->fd = open (name,O_CREAT|O_BINARY|O_WRONLY,S_IREAD|S_IWRITE);
  143.    if ((*pltfpp)->fd < 0) {
  144.       free ((void *)(*pltfpp)->buf);
  145.       free ((void*)*pltfpp);
  146.       *pltfpp = (PLTFILE *)NULL;
  147.       return (BADOPEN);
  148.    }
  149.    
  150.    /* now set buf_ptr to point to the beginning of the buffer so the first 
  151.       write will go into the buffer.
  152.    */
  153.    (*pltfpp)->buf_ptr = (*pltfpp)->buf;
  154.  
  155.    (*pltfpp)->buf_ctr = 0;
  156.    (*pltfpp)->eof = FALSE;
  157.    return (TRUE);
  158. }
  159.  
  160.  
  161.  
  162. /*
  163.    Function: closein
  164.    Purpose:  Discard the input buffer, close the input file, and deallocate 
  165.              the input buffer.
  166.  
  167.    Pre:  pltfpp is a pointer to a pointer to a PLTFILE.
  168.          If *pltfpp does not point to an open PLTFILE, it is NULL.
  169.  
  170.    Post:  If *pltfpp = NULL, TRUE is returned.
  171.           Otherwise, the contents of the input buffer are discarded, the 
  172.           input file is closed, and the input buffer is deallocated.
  173.           If an error occurs during the above process, FALSE is returned.
  174. */
  175.  
  176. int closein(pltfpp)
  177.    PLTFILE **pltfpp;
  178. {
  179.    if (*pltfpp == (PLTFILE *)NULL)
  180.       return (TRUE);
  181.    free ((void *)(*pltfpp)->buf);
  182.    if (close((*pltfpp)->fd) < 0) {
  183.        free ((void *)*pltfpp);
  184.        return (FALSE);
  185.    }
  186.    free ((void *)*pltfpp);
  187.    return (TRUE);
  188. }
  189.  
  190.  
  191.  
  192. /*
  193.    Function: closeout
  194.    Purpose:  Flush the output buffer, close the output file, and deallocate 
  195.              the output buffer.
  196.  
  197.    Pre:  pltfpp is a pointer to a pointer to a PLTFILE.
  198.          If *pltfpp does not point to an open PLTFILE, it is NULL.
  199.  
  200.    Post:  If *pltfpp = NULL, TRUE is returned.
  201.           Otherwise, the output buffer is flushed, the output file is closed, 
  202.           and the output buffer is deallocated.
  203.           If an error occurs during the above process, FALSE is returned.
  204. */
  205.  
  206. int closeout(pltfpp)
  207.    PLTFILE **pltfpp;
  208. {
  209.    if (*pltfpp == (PLTFILE *)NULL)
  210.       return (TRUE);
  211.    if (write((*pltfpp)->fd,(*pltfpp)->buf,(unsigned)(*pltfpp)->buf_ctr) != 
  212.        (*pltfpp)->buf_ctr) {
  213.       free ((void *)(*pltfpp)->buf);
  214.       (void) close ((*pltfpp)->fd);
  215.       free ((void *)*pltfpp);
  216.       return (FALSE);
  217.    }
  218.    free ((void *)(*pltfpp)->buf);
  219.    if (close((*pltfpp)->fd) < 0) {
  220.        free ((void *)*pltfpp);
  221.        return (FALSE);
  222.    }
  223.    free ((void *)*pltfpp);
  224.    return (TRUE);
  225. }
  226.  
  227.  
  228.  
  229. /*
  230.    Function: getstr
  231.    Purpose:  Read a string from the input buffer, possibly refilling the
  232.              buffer from the input file.  Data is read up to the next
  233.              line feed character.
  234.  
  235.    Pre:  pltfp is a pointer to a PLTFILE that has been opened for reading.
  236.          max is the maximum number of chars to be stored in string.
  237.          string is a pointer to storage for the input string.
  238.          There is enough storage available in string to hold "max"+1 chars.
  239.  
  240.    Post: An attempt is made to read a string from the input buffer and store
  241.          it in "string".
  242.          Reading stops at eof or the first line feed.  The line feed,
  243.          although included in the byte count, is replaced with a NULL.
  244.          If the input buffer is emptied during the process, the input file is 
  245.          read to refill the buffer.
  246.          If "max" chars are read without encountering a line feed, the extra
  247.          chars are discarded.
  248.          The number of bytes read from the input buffer is returned.
  249.          If end_of_file has already been reached, 0 is returned.
  250.          If an error occurs, -1 is returned.
  251. */
  252.  
  253. int getstr (pltfp, max, string)
  254.    PLTFILE *pltfp;
  255.    int max;
  256.    char *string;
  257. {
  258.    int cnt=0, done=FALSE;
  259.  
  260.    if (pltfp->eof == TRUE)
  261.       return (0);
  262.    if (pltfp->buf_ptr >= (pltfp->buf + pltfp->buf_ctr)) {
  263.       /* the buffer is empty, refill it from the input file */
  264.       switch (pltfp->buf_ctr = read(pltfp->fd,pltfp->buf,
  265.                                     (unsigned)BUFFSIZE)) {
  266.          case 0:
  267.             pltfp->eof = TRUE;
  268.             return (0);
  269.             break;
  270.          case -1:
  271.             return (-1);
  272.             break;
  273.          default:
  274.             pltfp->buf_ptr = pltfp->buf;
  275.             /* pltfp->buf_ctr was set by the read funtion */
  276.             break;
  277.       }
  278.    }
  279.    while (!done && cnt < max) {
  280.       if (pltfp->buf_ptr >= (pltfp->buf + pltfp->buf_ctr)) {
  281.          /* the buffer is empty, refill it from the input file */
  282.          switch (pltfp->buf_ctr = read(pltfp->fd,pltfp->buf,
  283.                                        (unsigned)BUFFSIZE)) {
  284.             case 0:
  285.                done = pltfp->eof = TRUE;
  286.                break;
  287.             case -1:
  288.                return (-1);
  289.                break;
  290.             default:
  291.                pltfp->buf_ptr = pltfp->buf;
  292.                /* pltfp->buf_ctr was set by the read funtion */
  293.                break;
  294.          }
  295.       }
  296.       if (!pltfp->eof) {
  297.          if ((*string = *(pltfp->buf_ptr)) == '\n') {
  298.             *string = (char)NULL;
  299.             cnt++;
  300.             pltfp->buf_ptr++;
  301.             done = TRUE;
  302.          } else {
  303.             string++;
  304.             cnt++;
  305.             pltfp->buf_ptr++;
  306.          }
  307.       }
  308.    }
  309.    if (!done) {
  310.       /* we have read max characters without finding a line feed.  Read until
  311.          line feed or eof, but don't store in string.
  312.       */
  313.       while (!done) {
  314.          if (pltfp->buf_ptr >= (pltfp->buf + pltfp->buf_ctr)) {
  315.             /* the buffer is empty, refill it from the input file */
  316.             switch (pltfp->buf_ctr = read(pltfp->fd,pltfp->buf,
  317.                                           (unsigned)BUFFSIZE)) {
  318.                case 0:
  319.                   done = pltfp->eof = TRUE;
  320.                   break;
  321.                case -1:
  322.                   return (-1);
  323.                   break;
  324.                default:
  325.                   pltfp->buf_ptr = pltfp->buf;
  326.                   /* pltfp->buf_ctr was set by the read funtion */
  327.                   break;
  328.            }
  329.          }
  330.          if (!pltfp->eof) {
  331.             if (*(pltfp->buf_ptr) == '\n')
  332.                done = TRUE;
  333.             cnt++;
  334.             pltfp->buf_ptr++;
  335.          }
  336.       }
  337.    }
  338.    return (cnt);
  339. }
  340.  
  341.  
  342.  
  343. /*
  344.    Function: putarr
  345.    Purpose:  Write a char array to the output buffer, possibly causing the 
  346.              buffer to be written to the output file.
  347.  
  348.    Pre: pltfp is a pointer to a PLTFILE that has been opened for writing.
  349.         num is the number of chars to be written to the output buffer.
  350.         chrarr is a pointer to the char array to be written.
  351.  
  352.    Post: An attempt is made to copy num chars from chrarr to the output 
  353.          buffer.
  354.          If the buffer becomes full during the process, an attempt is made
  355.          to write the buffer to the output file.
  356.          If an error occurs, FALSE is returned, otherwise TRUE is returned.
  357. */
  358.  
  359. int putarr (pltfp, num, chrarr)
  360.    PLTFILE *pltfp;
  361.    int num;
  362.    char *chrarr;
  363. {
  364.    int cnt=0;
  365.  
  366.    if (pltfp->buf_ptr >= pltfp->buf+BUFFSIZE) {
  367.       if (write(pltfp->fd,pltfp->buf,(unsigned)pltfp->buf_ctr) != pltfp->buf_ctr)
  368.          return (FALSE);
  369.       pltfp->buf_ptr = pltfp->buf;
  370.       pltfp->buf_ctr = 0;
  371.    }
  372.    while (cnt < num) {
  373.       if (pltfp->buf_ptr >= pltfp->buf+BUFFSIZE) {
  374.          if (write(pltfp->fd,pltfp->buf,(unsigned)pltfp->buf_ctr) != pltfp->buf_ctr)
  375.             return (FALSE);
  376.          pltfp->buf_ptr = pltfp->buf;
  377.          pltfp->buf_ctr = 0;
  378.       }
  379.       *(pltfp->buf_ptr++) = *chrarr++;
  380.       cnt++;
  381.       pltfp->buf_ctr++;
  382.    }
  383.    return (TRUE);
  384. }
  385.